package com.xiaoran.commonapi.controller;

import com.alipay.easysdk.factory.Factory;
import com.xiaoran.commonapi.tencent.Sms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
public class Test {
    @Autowired
    Sms sms;

    @GetMapping("/test")
    public String test() {
        sms.sendSms("13036473658");
        return "";
    }

    @PostMapping("/notify")
    public void notify(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<String> names = parameterMap.keySet();
        Map<String, String> requestParameters = getRequestParameters(request);
        try {
            if (Factory.Payment.Common().verifyNotify(requestParameters)) {
                System.out.println("验证成功");
            } else {
                System.out.println("验证失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*
            gmt_create[2022-03-14 20:51:23]
            charset[UTF-8]
            seller_email[uhrhqd7150@sandbox.com]
            subject[吃了么订单支付]
            sign[foR7VCeG2b5IYSboDIxVkugNSX7pQt7knAtnrL+qCsHKbSm2WzjBtnCSxtUDEaMk/0Xm41Xx3CHmSMGQlVLRK6qQHHrBTJXNsMRzjGudrIgtuOrggohdkOLeCKiucspPBJVgaFHZd9nIhqovh/9uZ0Vz8/yHEpQR8Ypg/mwJzpaLvIKKPNxLUIuXZP1Q/D/MWJsJ4+ySUtKxAjkr8ewf6XoJqvTOjgLAGOGWGrYNUHUops2D1c7fmwJOPdimS2Py8B88GejfFHt/HpyqQiFHGwr9TWFqGBiMueq3zmJ77OHPIbmK2UAuGRJimlj+NYAqpaMDnyJXknkgsBXcZbT9YQ==]
            buyer_id[2088622955503856]
            invoice_amount[15.80]
            notify_id[2022031400222205124003850518669621]
            fund_bill_list[[{"amount":"15.80","fundChannel":"ALIPAYACCOUNT"}]]
            notify_type[trade_status_sync]
            trade_status[TRADE_SUCCESS]
            receipt_amount[15.80]
            buyer_pay_amount[15.80]
            app_id[2021000117616890]
            sign_type[RSA2]
            seller_id[2088621955370813]
            gmt_payment[2022-03-14 20:51:23]
            notify_time[2022-03-14 20:51:24]
            version[1.0]
            out_trade_no[1ef127809b52c45cab60e2f03589d8eb]
            total_amount[15.80]
            trade_no[2022031422001403850501753902]
            auth_app_id[2021000117616890]
            buyer_logon_id[yki***@sandbox.com]
            point_amount[0.00]
         */
    }

    public Map<String, String> getRequestParameters(HttpServletRequest request) {
        String parameters = "";

        String totalParameter = "";
        Map<String, String[]> params = request.getParameterMap();
        int parametersNum = request.getParameterMap().size();//参数个数
        int flag = 1;
        for (String key : params.keySet()) {

            String[] values = params.get(key);
            for (int i = 0; i < values.length; i++) {
                String value = values[i];
                totalParameter += key + "=" + value;
            }
            if (flag < parametersNum) {
                totalParameter += "&";
            }
            flag += 1;
        }
        parameters = totalParameter;

        Map<String, String> map = new HashMap<String, String>();
        String[] arr = parameters.split("&");
        for (int i = 0; i < arr.length; i++) {
            String key = arr[i].substring(0, arr[i].indexOf("="));
            String value = arr[i].substring(arr[i].indexOf("=") + 1);
            map.put(key, value);
        }
        return map;
    }

}
